package com.jokerpan.project.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.PaginationBuilder;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.Route;
import com.jokerpan.project.mapper.RouteMapper;
import com.jokerpan.project.service.RouteService;
import com.jokerpan.project.util.RecursiveListUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author jokerpan
 */
@Service("routeService")
public class RouteServiceImpl implements RouteService {
    @Resource
    RouteMapper routeMapper;


    @Override
    public Map<String, Object> queryList(Route route) {
        List<LinkedHashMap<String, Object>> resultList;
        Page<Object> page = PageHelper.startPage(route.getCurrentPage(), route.getPageSize());
        List<LinkedHashMap<String, Object>> list = this.routeMapper.queryList(route);
        list.forEach(e -> {
            e.put("hideInMenu", e.get("hideInMenu").toString().equals("1") ? false : true);
        });
        resultList = RecursiveListUtils.queryRecursiveList(list);
        return PaginationBuilder.buildResult(resultList, page.getTotal(), route.getCurrentPage(), route.getPageSize());
    }

    @Override
    public List<LinkedHashMap<String, Object>> queryAll(Integer roleId) {
        List<LinkedHashMap<String, Object>> list = this.routeMapper.queryAll(roleId);
        list.forEach(e -> {
            e.put("hideInMenu", e.get("hideInMenu").toString().equals("1") ? false : true);
        });
        return RecursiveListUtils.queryRecursiveList(list);
    }

    @Override
    public Boolean add(Route route) {
        return routeMapper.add(route) > 0;
    }

    @Override
    public ActionResult update(Route route) {
        if (routeMapper.update(route) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @Override
    public ActionResult deleteById(Integer id) {
        if (routeMapper.deleteById(id) > 0){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }
}
